package com.xxh.cloud.provider.until;

import org.redisson.Redisson;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;

import java.util.Map;
import java.util.concurrent.TimeUnit;

public class RedissonUtil {
    private static RedissonClient client;
    static {
        Config config=new Config();
        config.useSingleServer().setAddress("redis://39.105.189.141:6380").setPassword("qfjava");
        client=Redisson.create(config);
    }
    //新增
    public static void addStr(String key,Object val){
        client.getBucket(key).set(val);
    }
    public static void addStrTime(String key,Object val,long seconds){
        client.getBucket(key).set(val,seconds, TimeUnit.SECONDS);
    }
    public static void addSet(String key,Object val){
        client.getSet(key).add(val);
    }
    public static void addHash(String key,String field,Object val){
        client.getMap(key).put(field,val);
    }
    public static void addHashs(String key,Map<String,Object> map){
        client.getMap(key).putAll(map);
    }
    //校验
    public static boolean checkSet(String key,Object val){
        return client.getSet(key).contains(val);
    }
    public static boolean checkHash(String key,String field){
        return client.getMap(key).containsKey(field);
    }
    //查询
    public static Map<Object,Object> getMap(String key){
        return client.getMap(key).readAllMap();
    }
    public static int getMapSize(String key){
        return client.getMap(key).size();
    }
    public static Object getStr(String key){
        return client.getBucket(key).get();
    }
    //删除
    public static boolean delHash(String key,Object field){
        return client.getMap(key).remove(field)!=null;
    }

    //系统命令
    public static boolean checkKeys(String... keys){
        return client.getKeys().countExists(keys)>0;
    }
    public static long ttl(String key){
        return client.getKeys().remainTimeToLive(key);
    }
    public static boolean delKeys(String... keys){
        return client.getKeys().delete(keys)>0;
    }

    public static void expire(String key,long seconds){
        client.getKeys().expire(key,seconds,TimeUnit.SECONDS);
    }
    //分布式锁
    public static RLock getLock(String key){
        return client.getLock(key);
    }
}
